package org.ace.model
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.ace.astar.AStar;
	import org.ace.astar.Grid;
	import org.ace.astar.WayPoint;
	import org.ace.communicate.MessageReceived;
	import org.ace.protocol.MapProtocol;
	import org.ace.protocol.Protocol;
	import org.ace.view.BattleHandler;
	import org.ace.view.GameSceneHandler;

	public class RoleModel extends Sprite
	{
		public var id:int;
		public var mx:Number;
		public var my:Number;
		public var level:int;
		public var exp:int;
		public var expMax:int;
		public var attack:int;
		public var magic:int;
		public var model:int;
		public var mname:String;
		public var mapId:int;
		public var sp:MovieClip;
		
		public function RoleModel()
		{
		}
		private var wayIn:Boolean=false;
		private var wayIndex:int=0;
		private var speed:Number=6;
		private var moveArray:Array;
		private var w:int=16;
		private var h:int=16;
		private var grid:Grid;
		public function onFrame(e:Event):void{
			
			if(wayIndex==moveArray.length){
//				switch(sp.currentFrame){
//					case 1:
//						sp.gotoAndStop(5);
//						break;
//					case 2:
//						sp.gotoAndStop(6);
//						break;
//					case 3:
//						sp.gotoAndStop(7);
//						break;
//					case 4:
//						sp.gotoAndStop(8);
//						break;
//				}
				sp.removeEventListener(Event.ENTER_FRAME,onFrame);
				wayIn=false;
				return;
			}
			sp.x=moveArray[wayIndex][0];
			sp.y=moveArray[wayIndex][1];
			switch(moveArray[wayIndex][2]){
				case AStar.RIGHT:
					if(sp.currentFrame!=4){
						sp.gotoAndStop(4);
					}
					break;
				case AStar.LOWER_RIGHT:
					if(sp.currentFrame!=7){
						sp.gotoAndStop(7);
					}
					break;
				case AStar.UPPER_RIGHT:
					if(sp.currentFrame!=8){
						sp.gotoAndStop(8);
					}
						break;
				case AStar.LEFT:
					if(sp.currentFrame!=2){
						sp.gotoAndStop(2);
					}
					break;
				case AStar.LOWER_LEFT:
					if(sp.currentFrame!=6){
						sp.gotoAndStop(6);
					}
					break;
				case AStar.UPPER_LEFT:
					if(sp.currentFrame!=5){
						sp.gotoAndStop(5);
					}
					break;
				case AStar.UP:
					if(sp.currentFrame!=1){
						sp.gotoAndStop(1);
					}
					break;
				case AStar.DOWN:
					if(sp.currentFrame!=3){
						sp.gotoAndStop(3);
					}
					break;
			}
			wayIndex++;
			moveSize++;
			if(moveSize%10==0){
				
				GameSceneHandler.getScene().resetRolez();
			}
			var o:Object=grid.getNode( AStar.isoToGrid(sp.x,w),
			AStar.isoToGrid(sp.y,h)).definition;
			if(o!=null){
				if(o.type=="Gate"){
					if(id==MyModel.id){
						MessageReceived.getMessageReceived().write(Protocol.TYPE_MAP,MyModel.mapId,MapProtocol.leave_creq);
						GameSceneHandler.getScene().clear();
						MyModel.mapId=o.id;
						var a:Array=new Array();
						var ax:int=o.x;
						var ay:int=o.y;
						a.push(ax);
						a.push(ay);
						MessageReceived.getMessageReceived().write(Protocol.TYPE_MAP,MyModel.mapId,MapProtocol.enter,a);
						sp.removeEventListener(Event.ENTER_FRAME,onFrame);
						return;
					}
				}
			}
			if(id==MyModel.id){
				if(-(sp.x-(960/2))>0){
					GameSceneHandler.getScene().x=0;
				}else if(-(sp.x-(960/2))-960<-GameSceneHandler.getScene().width){
					GameSceneHandler.getScene().x=-GameSceneHandler.getScene().width+960;
				}else{
					GameSceneHandler.getScene().x=-(sp.x-(960/2));
					
				}
				if(-(sp.y-(560/2))>0){
					GameSceneHandler.getScene().y=0;
				}else if(-(sp.y-(560/2))-560<-GameSceneHandler.getScene().height){
					GameSceneHandler.getScene().y=-GameSceneHandler.getScene().height+560;
				}else{
					GameSceneHandler.getScene().y=-(sp.y-(560/2));
				
				}
				if(moveSize>=170){
					moveSize=1;
					if(GameSceneHandler.getScene().mapType==1){
						var i:int=Math.random()*100;
						if(i>50){
							var xy:Array=new Array();
							xy.push(sp.x);
							xy.push(sp.y);
							MessageReceived.getMessageReceived().write(Protocol.TYPE_MAP,MyModel.mapId,MapProtocol.on_battle_creq,xy);
							return;
						}
					}
				}
				
			}
		}
		
		public function onBattle(isMy:Boolean):void{
			sp.removeEventListener(Event.ENTER_FRAME,onFrame);
			wayIn=false;
			if(isMy){
				GameSceneHandler.getScene().setRcV(false);
				BattleHandler.getScene().process(null);
			}
		}
		
		private var moveSize:int=0;
		public function move(gridArray:Array,g:Grid):void{
			grid=g;
			if(gridArray.length>0){
				moveArray=caclPixPath(gridArray);
				wayIndex=0;
				if(!wayIn){
					wayIn=true;
					sp.addEventListener(Event.ENTER_FRAME,onFrame);
				}
				
			}
		}
		public function delet():void{
			sp.removeEventListener(Event.ENTER_FRAME,onFrame);
			sp.parent.removeChild(sp);
		}
	
		
		/**
		 *将网格数组转换为每一步的像素点数组 
		 */
		private function caclPixPath(/*oper:Sprite,*/ wayPoints:Array):Array 
		{ 
			var sx:Number = sp.x; 
			var sy:Number = sp.y; 
			var pixPath:Array = new Array(); 
			
			var direction:int//方向 
			
			for each(var point:Object in wayPoints) 
			{ 
				var tx:Number = point.x; 
				var ty:Number = point.y; 
				
				//判定当前方向 
				var xpos:Number = tx - sx; 
				var ypos:Number = ty - sy; 
				direction = AStar.getDirection(xpos, ypos); 
				
				//速度判定 
				var distance:Number = Math.sqrt(xpos*xpos + ypos*ypos); 
				var dpos:uint = uint(distance/speed); 
				
				//dpos每帧移动的速度 
				if(dpos==0)//移动偏移量过小 直接插入	 
				{ 
					pixPath.push([sx, sy, direction]); 
				} 
				else 
					
				{ 
					//计算每帧移动的速度 
					var speedX:Number = xpos / dpos; 
					var speedY:Number = ypos / dpos; 
					//循环 压入栈 
					for(var i:int = 1; i<=dpos; i++) 
					{ 
						sx += speedX; 
						sy += speedY; 
						pixPath.push([sx, sy, direction]); 
					} 
					//记录完结时本节点的坐标 
					sx = tx; 
					sy = ty; 
				} 
			} 
			//修正终结点的坐标 以防止出现终结点坐标偏移的情况 
			var pix:Object = pixPath[pixPath.length - 1]; 
			pix[0] = point.x; 
			pix[1] = point.y; 
			return pixPath; 
		} 
	}
}